package com.saifan.wyy_ov.ui.communitylife;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.ColletionRequestBean;
import com.saifan.wyy_ov.data.bean.ConvenienttooBean;
import com.saifan.wyy_ov.data.bean.PermissionsBean;
import com.saifan.wyy_ov.data.bean.RequestBean;
import com.saifan.wyy_ov.data.bean.RoomInfoBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.RadioGroup;
import com.saifan.wyy_ov.utils.CallPhoneUtil;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.LogUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-13 20:19:18
 * @descrption 便民工具
 */

public class ConvenienttooActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {
    private Toolbar toolbar;
    private ListView listView;
    private RadioGroup radiogroup;
    private RadioButton radioButton;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private RadioButton radioButton4;
    private RadioButton radioButton5;
    private RadioButton radioButton6;
    private RadioButton radioButton7;
    private RadioButton radioButton8;
    private RadioButton radioButton9;
    private RadioButton radioButton10;

    RoomInfoBean roomInfoBean;
    int type = 11;
    List<ConvenienttooBean> convenienttooBeans = new ArrayList<ConvenienttooBean>();
    CommonAdapter adapter;
    private RequestBean requestBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        roomInfoBean = getRoomInfoBean();


        setContentView();
        initAdapger();
        query(type);
    }


    private void query(int typeID) {
        GetData getData = new GetData();
        requestBean = new RequestBean();
        requestBean.setXMBS(roomInfoBean.getXMBS());
        requestBean.setUserID(roomInfoBean.getFKGX_ZJ());
        requestBean.setType(typeID);
        getData.getDataJson(this, Api.Customer.CONVENIENT_TOOL, requestBean, getString(R.string.wait), new ResponseData() {
            @Override
            public void onSuccess(String json) {
                Gson gson = new Gson();
                List<ConvenienttooBean> list = gson.fromJson(json, new TypeToken<List<ConvenienttooBean>>() {
                }.getType());

                if (list != null && !list.isEmpty()) {
                    convenienttooBeans = list;

                } else {
                    convenienttooBeans.clear();
                }
                adapter.mDatas = convenienttooBeans;
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onFailed(String str) {
                LogUtil.e(str);

            }
        });
    }

    private void initAdapger() {
        adapter = new CommonAdapter<ConvenienttooBean>(this, convenienttooBeans, R.layout.convenienttoo_item) {
            @Override
            public void convert(final ViewHolderHelper helper, final ConvenienttooBean item) {

                helper.setText(R.id.BMGJ_LXR, StringUtil.returnShow(item.getBMGJ_BT()));
                helper.setText(R.id.BMGJ_LXDH, StringUtil.returnShow(item.getBMGJ_LXDH()));
                helper.setText(R.id.BMGJ_DZ, "  " + StringUtil.returnShow(item.getBMGJ_DZ()));
                helper.setText(R.id.BMGJ_FWSM, StringUtil.returnShow(item.getBMGJ_FWSM()));
                if (type == 11) {
                    helper.getView(R.id.BMGJ_FWSM).setVisibility(View.GONE);
                } else {
                    helper.getView(R.id.BMGJ_FWSM).setVisibility(View.VISIBLE);
                }
                if (!item.isBMGJ_WYRZ()) {
                    helper.getView(R.id.sign_authentication).setVisibility(View.GONE);
                }else {
                    helper.getView(R.id.sign_authentication).setVisibility(View.VISIBLE);
                }
                helper.getView(R.id.collect_tel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (getRoomInfoBean().getUserType() == 2) {
                            for (PermissionsBean permissionsBean : getPermissions()) {
                                if (permissionsBean.getYKQX_GNMC().equals("便民工具")) {
                                    v.setClickable(false);
                                    if (item.isBMGJ_SFYX()) {
                                        ColletionRequestBean colletionRequestBean = new ColletionRequestBean();
                                        colletionRequestBean.setXMBS(roomInfoBean.getXMBS());
                                        colletionRequestBean.setWDSC_KHWJ(roomInfoBean.getFKGX_ZJ());
                                        colletionRequestBean.setWDSC_MKID(item.getBMGJ_ZJ());
                                        colletionRequestBean.setWDSC_LX(1);


                                        new GetData().getDataJson(ConvenienttooActivity.this, Api.Collection.CANCEL_COLLECTION, colletionRequestBean, getString(R.string.wait), new ResponseData() {
                                            @Override
                                            public void onSuccess(String json) {
                                                item.setBMGJ_SFYX(false);
                                                helper.getView(R.id.imageView_collect_tel).setBackgroundResource(R.drawable.collect_abolish);
                                                ((TextView) (helper.getView(R.id.textView_collect_tel))).setText("收藏");
                                                ((TextView) (helper.getView(R.id.textView_collect_tel))).setTextColor(getResources().getColor(R.color.secand_black));
                                                helper.getView(R.id.collect_tel).setClickable(true);
                                            }

                                            @Override
                                            public void onFailed(String str) {
                                                helper.getView(R.id.collect_tel).setClickable(true);
                                            }
                                        });

                                    } else {

                                        ColletionRequestBean colletionRequestBean = new ColletionRequestBean();
                                        colletionRequestBean.setXMBS(roomInfoBean.getXMBS());
                                        colletionRequestBean.setWDSC_KHWJ(roomInfoBean.getFKGX_ZJ());
                                        colletionRequestBean.setWDSC_MKID(item.getBMGJ_ZJ());
                                        colletionRequestBean.setWDSC_LX(1);


                                        new GetData().getDataJson(ConvenienttooActivity.this, Api.Collection.COLLECTION, colletionRequestBean, getString(R.string.wait), new ResponseData() {
                                            @Override
                                            public void onSuccess(String json) {
                                                item.setBMGJ_SFYX(true);
                                                helper.getView(R.id.imageView_collect_tel).setBackgroundResource(R.drawable.collect_succeed);
                                                ((TextView) (helper.getView(R.id.textView_collect_tel))).setText("取消收藏");
                                                ((TextView) (helper.getView(R.id.textView_collect_tel))).setTextColor(getResources().getColor(R.color.china_red));
                                                helper.getView(R.id.collect_tel).setClickable(true);
                                            }

                                            @Override
                                            public void onFailed(String str) {
                                                helper.getView(R.id.collect_tel).setClickable(true);
                                            }
                                        });
                                    }
                                    return;
                                }
                            }
                            ToastUtil.showToast(ConvenienttooActivity.this, "您的权限不能进行此操作");
                            return;
                        } else if (getRoomInfoBean().getUserType() == 3) {
                            ToastUtil.showToast(ConvenienttooActivity.this, "您的权限不能进行此操作");
                            return;
                        }
                        v.setClickable(false);
                        if (item.isBMGJ_SFYX()) {
                            ColletionRequestBean colletionRequestBean = new ColletionRequestBean();
                            colletionRequestBean.setXMBS(roomInfoBean.getXMBS());
                            colletionRequestBean.setWDSC_KHWJ(roomInfoBean.getFKGX_ZJ());
                            colletionRequestBean.setWDSC_MKID(item.getBMGJ_ZJ());
                            colletionRequestBean.setWDSC_LX(1);


                            new GetData().getDataJson(ConvenienttooActivity.this, Api.Collection.CANCEL_COLLECTION, colletionRequestBean, getString(R.string.wait), new ResponseData() {
                                @Override
                                public void onSuccess(String json) {
                                    item.setBMGJ_SFYX(false);
                                    helper.getView(R.id.imageView_collect_tel).setBackgroundResource(R.drawable.collect_abolish);
                                    ((TextView) (helper.getView(R.id.textView_collect_tel))).setText("收藏");
                                    ((TextView) (helper.getView(R.id.textView_collect_tel))).setTextColor(getResources().getColor(R.color.secand_black));
                                    helper.getView(R.id.collect_tel).setClickable(true);
                                }

                                @Override
                                public void onFailed(String str) {
                                    helper.getView(R.id.collect_tel).setClickable(true);
                                }
                            });

                        } else {

                            ColletionRequestBean colletionRequestBean = new ColletionRequestBean();
                            colletionRequestBean.setXMBS(roomInfoBean.getXMBS());
                            colletionRequestBean.setWDSC_KHWJ(roomInfoBean.getFKGX_ZJ());
                            colletionRequestBean.setWDSC_MKID(item.getBMGJ_ZJ());
                            colletionRequestBean.setWDSC_LX(1);


                            new GetData().getDataJson(ConvenienttooActivity.this, Api.Collection.COLLECTION, colletionRequestBean, getString(R.string.wait), new ResponseData() {
                                @Override
                                public void onSuccess(String json) {
                                    item.setBMGJ_SFYX(true);
                                    helper.getView(R.id.imageView_collect_tel).setBackgroundResource(R.drawable.collect_succeed);
                                    ((TextView) (helper.getView(R.id.textView_collect_tel))).setText("取消收藏");
                                    ((TextView) (helper.getView(R.id.textView_collect_tel))).setTextColor(getResources().getColor(R.color.china_red));
                                    helper.getView(R.id.collect_tel).setClickable(true);
                                }

                                @Override
                                public void onFailed(String str) {
                                    helper.getView(R.id.collect_tel).setClickable(true);
                                }
                            });
                        }

                    }
                });

                if (item.isBMGJ_SFYX()) {
                    helper.getView(R.id.imageView_collect_tel).setBackgroundResource(R.drawable.collect_succeed);
                    ((TextView) (helper.getView(R.id.textView_collect_tel))).setText("取消收藏");
                    ((TextView) (helper.getView(R.id.textView_collect_tel))).setTextColor(getResources().getColor(R.color.china_red));
                } else {
                    helper.getView(R.id.imageView_collect_tel).setBackgroundResource(R.drawable.collect_abolish);
                    ((TextView) (helper.getView(R.id.textView_collect_tel))).setText("收藏");
                    ((TextView) (helper.getView(R.id.textView_collect_tel))).setTextColor(getResources().getColor(R.color.secand_black));
                }


                final String tel = item.getBMGJ_LXDH();
                helper.getView(R.id.tel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (Build.VERSION.SDK_INT >= 23) {

                            int checkCallPhonePermission = ContextCompat.checkSelfPermission( ConvenienttooActivity.this, Manifest.permission.CALL_PHONE);
                            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                                ActivityCompat.requestPermissions( ConvenienttooActivity.this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE);
                                return;
                            } else {
                                CallPhoneUtil.callPhone( ConvenienttooActivity.this,  tel);


                            }
                        } else {

                            CallPhoneUtil.callPhone( ConvenienttooActivity.this, tel);

                        }


                    }
                });
                helper.getView(R.id.cardView).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(ConvenienttooActivity.this, ConvenienttooInfoActivity.class);
                        intent.putExtra("ConvenienttooBean", item);
                        startActivity(intent);

                    }
                });

            }
        };
        listView.setAdapter(adapter);
    }

    private static final int CALL_PHONE = 123;
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == CALL_PHONE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {




            } else {
                // Permission Denied
                Toast.makeText(this, "您禁用拨打电话权限", Toast.LENGTH_SHORT)
                        .show();

            }
        }

    }
    public void setContentView() {
        setContentView(R.layout.activity_convenienttoo);
        radiogroup = (RadioGroup) getLayoutInflater().inflate(R.layout.convenienttoo_radio_group_view, null);
        this.radiogroup = (RadioGroup) radiogroup.findViewById(R.id.radiogroup);
        this.radioButton10 = (RadioButton) radiogroup.findViewById(R.id.radioButton10);
        this.radioButton9 = (RadioButton) radiogroup.findViewById(R.id.radioButton9);
        this.radioButton8 = (RadioButton) radiogroup.findViewById(R.id.radioButton8);
        this.radioButton7 = (RadioButton) radiogroup.findViewById(R.id.radioButton7);
        this.radioButton6 = (RadioButton) radiogroup.findViewById(R.id.radioButton6);
        this.radioButton5 = (RadioButton) radiogroup.findViewById(R.id.radioButton5);
        this.radioButton4 = (RadioButton) radiogroup.findViewById(R.id.radioButton4);
        this.radioButton3 = (RadioButton) radiogroup.findViewById(R.id.radioButton3);
        this.radioButton2 = (RadioButton) radiogroup.findViewById(R.id.radioButton2);
        this.radioButton1 = (RadioButton) radiogroup.findViewById(R.id.radioButton1);
        this.radioButton = (RadioButton) radiogroup.findViewById(R.id.radioButton);
        this.listView = (ListView) findViewById(R.id.listView);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        listView.addHeaderView(radiogroup);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        radiogroup.setOnCheckedChangeListener(this);
        radioButton.setOnCheckedChangeListener(this);
        radioButton1.setOnCheckedChangeListener(this);
        radioButton2.setOnCheckedChangeListener(this);
        radioButton3.setOnCheckedChangeListener(this);
        radioButton4.setOnCheckedChangeListener(this);
        radioButton5.setOnCheckedChangeListener(this);
        radioButton6.setOnCheckedChangeListener(this);
        radioButton7.setOnCheckedChangeListener(this);
        radioButton8.setOnCheckedChangeListener(this);
        radioButton9.setOnCheckedChangeListener(this);
        radioButton10.setOnCheckedChangeListener(this);


    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {


    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            int i = buttonView.getId();
            if (i == R.id.radioButton) {
                type = 11;

            } else if (i == R.id.radioButton1) {
                type = 1;

            } else if (i == R.id.radioButton2) {
                type = 2;

            } else if (i == R.id.radioButton3) {
                type = 3;

            } else if (i == R.id.radioButton4) {
                type = 4;

            } else if (i == R.id.radioButton5) {
                type = 5;

            } else if (i == R.id.radioButton6) {
                type = 6;

            } else if (i == R.id.radioButton7) {
                type = 7;

            } else if (i == R.id.radioButton8) {
                type = 8;

            } else if (i == R.id.radioButton9) {
                type = 9;

            } else if (i == R.id.radioButton10) {
                type = 10;

            } else {
            }
            query(type);
        }

    }
}
